<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS盒子模型实例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f5f7fa;
            padding: 20px;
        }
        
        .container {
            max-width: 1000px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }
        
        .content {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .theory {
            flex: 1;
            min-width: 300px;
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        
        .box-model-demo {
            flex: 1;
            min-width: 300px;
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        
        h2 {
            color: #2c3e50;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f0f0f0;
        }
        
        h3 {
            color: #3498db;
            margin: 20px 0 10px;
        }
        
        p {
            margin-bottom: 15px;
        }
        
        ul {
            margin-left: 20px;
            margin-bottom: 20px;
        }
        
        li {
            margin-bottom: 8px;
        }
        
        .box-container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 300px;
            background-color: #f8f9fa;
            border-radius: 8px;
            margin: 20px 0;
            position: relative;
        }
        
        .box {
            width: 200px;
            height: 150px;
            background-color: #3498db;
            color: white;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            text-align: center;
            padding: 20px;
            border: 10px solid #e74c3c;
            margin: 30px;
            position: relative;
            border-radius: 5px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        
        .content-area {
            background-color: rgba(255, 255, 255, 0.2);
            padding: 10px;
            border-radius: 3px;
            width: 100%;
        }
        
        .dimension-label {
            position: absolute;
            background: white;
            padding: 2px 6px;
            border-radius: 3px;
            font-size: 12px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
            color: #333;
        }
        
        .margin-label {
            top: 10px;
            right: 10px;
            background: #9b59b6;
            color: white;
        }
        
        .border-label {
            bottom: 10px;
            left: 10px;
            background: #e74c3c;
            color: white;
        }
        
        .padding-label {
            top: 50%;
            right: -40px;
            transform: translateY(-50%);
            background: #f1c40f;
            color: #333;
        }
        
        .content-label {
            bottom: -25px;
            right: 10px;
            background: #3498db;
            color: white;
        }
        
        .code-example {
            background: #2c3e50;
            color: #ecf0f1;
            padding: 15px;
            border-radius: 5px;
            font-family: 'Courier New', monospace;
            margin: 20px 0;
            overflow-x: auto;
        }
        
        .property {
            color: #3498db;
        }
        
        .value {
            color: #e74c3c;
        }
        
        .comment {
            color: #7f8c8d;
        }
        
        .box-types {
            display: flex;
            gap: 20px;
            margin-top: 30px;
        }
        
        .box-type {
            flex: 1;
            padding: 15px;
            border-radius: 5px;
            text-align: center;
        }
        
        .standard-box {
            background: #d5f5e3;
            border: 2px dashed #27ae60;
        }
        
        .border-box {
            background: #e8f4fd;
            border: 2px dashed #3498db;
        }
        
        footer {
            text-align: center;
            margin-top: 30px;
            padding: 20px;
            background-color: #34495e;
            color: white;
            border-radius: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>CSS盒子模型详解</h1>
            <p class="subtitle">理解网页布局的基础概念</p>
        </header>
        
        <div class="content">
            <div class="theory">
                <h2>盒子模型概念</h2>
                <p>在CSS中，每个元素都被表示为一个矩形的盒子。这个盒子由四个部分组成，从内到外分别是：</p>
                
                <h3>1. 内容区域 (Content)</h3>
                <p>显示元素的实际内容，如文本、图像等。尺寸通过 <strong>width</strong> 和 <strong>height</strong> 属性控制。</p>
                
                <h3>2. 内边距 (Padding)</h3>
                <p>内容区域与边框之间的透明区域。使用 <strong>padding</strong> 属性设置。</p>
                
                <h3>3. 边框 (Border)</h3>
                <p>围绕内容和内边距的边界线。使用 <strong>border</strong> 属性设置。</p>
                
                <h3>4. 外边距 (Margin)</h3>
                <p>盒子与其他元素之间的透明间距。使用 <strong>margin</strong> 属性设置。</p>
                
                <h2>盒子模型类型</h2>
                <ul>
                    <li><strong>标准盒子模型 (content-box)</strong>: width和height只定义内容区域的尺寸</li>
                    <li><strong>IE盒子模型 (border-box)</strong>: width和height定义内容、内边距和边框的总尺寸</li>
                </ul>
                
                <div class="code-example">
                    <span class="comment">/* 设置盒子模型类型 */</span><br>
                    <span class="property">box-sizing</span>: <span class="value">content-box</span>; <span class="comment">/* 默认值，标准盒子模型 */</span><br>
                    <span class="property">box-sizing</span>: <span class="value">border-box</span>; <span class="comment">/* IE盒子模型 */</span>
                </div>
            </div>
            
            <div class="box-model-demo">
                <h2>盒子模型可视化</h2>
                <p>下面的示例展示了CSS盒子模型的各个组成部分：</p>
                
                <div class="box-container">
                    <div class="box">
                        <div class="content-area">
                            <strong>内容区域</strong><br>
                            width: 200px<br>
                            height: 150px
                        </div>
                    </div>
                    <div class="dimension-label margin-label">外边距: 30px</div>
                    <div class="dimension-label border-label">边框: 10px</div>
                    <div class="dimension-label padding-label">内边距: 20px</div>
                    <div class="dimension-label content-label">内容区域</div>
                </div>
                
                <div class="code-example">
                    <span class="comment">/* CSS代码示例 */</span><br>
                    .box {<br>
                    &nbsp;&nbsp;<span class="property">width</span>: <span class="value">200px</span>;<br>
                    &nbsp;&nbsp;<span class="property">height</span>: <span class="value">150px</span>;<br>
                    &nbsp;&nbsp;<span class="property">padding</span>: <span class="value">20px</span>;<br>
                    &nbsp;&nbsp;<span class="property">border</span>: <span class="value">10px solid #e74c3c</span>;<br>
                    &nbsp;&nbsp;<span class="property">margin</span>: <span class="value">30px</span>;<br>
                    &nbsp;&nbsp;<span class="property">background-color</span>: <span class="value">#3498db</span>;<br>
                    }
                </div>
                
                <h3>盒子模型类型对比</h3>
                <div class="box-types">
                    <div class="box-type standard-box">
                        <h4>标准盒子模型</h4>
                        <p>元素总宽度 = width + padding-left + padding-right + border-left + border-right + margin-left + margin-right</p>
                        <p>200px + 20px + 20px + 10px + 10px + 30px + 30px = <strong>320px</strong></p>
                    </div>
                    <div class="box-type border-box">
                        <h4>IE盒子模型</h4>
                        <p>元素总宽度 = width + margin-left + margin-right</p>
                        <p>200px + 30px + 30px = <strong>260px</strong></p>
                        <p>(内容区域会自动调整)</p>
                    </div>
                </div>
            </div>
        </div>
        
        <footer>
            <p>CSS盒子模型是网页布局的基础，掌握它对于创建精确的网页设计至关重要。</p>
        </footer>
    </div>
</body>
</html>